<?php
class TextAction extends Action{
	
	public function index(){
		
		$this->display();
	}
	
	
	/**
	 * 添加文章
	 */
	public function add()
	{
		//默认显示添加表单
		if (!IS_POST) {
			$this->assign("category",(M('category')->select()));
			$this->display();
		}
		if (IS_POST) {
			//如果用户提交数据
			$model = D("Post");
			$model->time = time();
			//$model->user_id = 1;
			
			if (!$model->create()) {
				// 如果创建失败 表示验证没有通过 输出错误提示信息
				echo $this->error($model->getError());
				exit();
			} else {
				if ($model->add()) {
					$this->success("添加成功", U('text/index'));
					echo "======abc====";
				} else {
					$this->error("添加失败");
				}
			}
		}
	}
	
	
	/**
	 * 获取排序后的分类
	 * @param  [type]  $data  [description]
	 * @param  integer $pid   [description]
	 * @param  string  $html  [description]
	 * @param  integer $level [description]
	 * @return [type]         [description]
	 */
	function getSortedCategory($data,$pid=0,$html="|---",$level=0)
	{
		$temp = array();
		foreach ($data as $k => $v) {
			if($v['pid'] == $pid){
	
				$str = str_repeat($html, $level);
				$v['html'] = $str;
				$temp[] = $v;
	
				$temp = array_merge($temp,getSortedCategory($data,$v['id'],'|---',$level+1));
			}
	
		}
		return $temp;
	}
	
}